package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BTask;
import com.ruoyi.system.domain.vo.BTaskVo;
import com.ruoyi.system.service.BTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 任务管理
 * @author yangxz
 **/
@RestController
@RequestMapping("/business/task")
public class TaskController extends BaseController {

    @Autowired
    BTaskService taskService;

    /**
     * 获取任务列表
     */
    @PreAuthorize("@ss.hasPermi('dict:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(BTask task)
    {
        startPage();
        List<BTaskVo> list = taskService.selectTaskList(task);
        return getDataTable(list);
    }

    /**
     * 根据任务Id获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('dict:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable Long taskId) {
        BTaskVo taskVo = taskService.selectTaskById(taskId);
        return success(taskVo);
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('dict:task:add')")
    @Log(title = "新增任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BTask task)
    {
        task.setCreateBy(getUsername());
        task.setCreateTime(new Date());
        return toAjax(taskService.save(task));
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('dict:task:edit')")
    @Log(title = "修改任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BTask task)
    {
        task.setUpdateBy(getUsername());
        task.setUpdateTime(new Date());
        return toAjax(taskService.updateById(task));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('dict:task:remove')")
    @Log(title = "删除任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds)
    {
        return toAjax(taskService.deleteTaskByIds(taskIds));
    }


}
